package com.hystrix.requestmerge;

import com.hystrix.entity.User;
import com.hystrix.service.UserServiceMerge;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 测试请求合并
 * */
@Service
public class UserServiceMergeImpl implements UserServiceMerge{

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCollapser(batchMethod = "findAll",
                      collapserProperties = {@HystrixProperty(name = "timerDelayInMilliseconds",value = "100")})
    @Override
    public User find(int id) {
        return restTemplate.getForObject("http://provider-server/getUser/"+id,User.class);
    }

    @HystrixCommand
    @Override
    public List<User> findAll(List<Integer> ids) {
        return restTemplate.getForObject("http://provider-server/getUser/1",List.class,
                StringUtils.join(ids,","));
    }

}
